package com.gouwudai.gobbs.dao.impl;

import org.apache.log4j.Logger;

import com.gouwudai.gobbs.dao.ISysInfoDAO;
import com.gouwudai.gobbs.pojo.TbbsSysInfo;
import com.litt.core.cache.OSCacheManager;

public class SysInfoDAOImpl extends BaseDAOImpl implements ISysInfoDAO
{
	private static final Logger logger = Logger.getLogger(SysInfoDAOImpl.class);
	
	private static final int SERIAL_NO = 1;	//系统编号，目前只为1
	
	
	/**
     * 读取系统信息 
	 */
    public TbbsSysInfo load() 
	{
        TbbsSysInfo tbbsSysInfo = (TbbsSysInfo)OSCacheManager.loadCacheObject("SYS_INFO");
        if(tbbsSysInfo==null)
        {
            tbbsSysInfo = (TbbsSysInfo)super.loadObject(TbbsSysInfo.class,SERIAL_NO);  
            OSCacheManager.storeCacheObject("SYS_INFO", tbbsSysInfo);
        }    
        return tbbsSysInfo;			
	}
	
    /**
     * 更新系统信息
     * @param sysInfo
     */
	public void update(TbbsSysInfo sysInfo) 
	{
		    StringBuilder hql = new StringBuilder("update TbbsSysInfo set sysName=?");
	        hql.append(",sysUrl=?");
	        hql.append(",sysTitle=?");
	        hql.append(",sysMetaKey=?");
	        hql.append(",sysMetaDesc=?");
	        hql.append(",copyright=?");
	        hql.append(",smtpServer=?");
	        hql.append(",smtpId=?");
	        hql.append(",smtpPass=?");
	        hql.append(",smtpAuth=?");
	        hql.append(",smtpPort=?");
	        hql.append(",smtpSsl=?");
	        hql.append(",sysEmail=?");
	        hql.append(",sysEmailSign=?");
	        hql.append(",upfileOpenFlag=?");
	        hql.append(",upfileMaxSize=?");
	        hql.append(",upfileDefaultSize=?");
	        hql.append(",upfileExt=?");
	        hql.append(",version=?");
	        hql.append(",userpicOpenFlag=?");
	        hql.append(",refreshPage=?");
	        hql.append(",isHottopic=?");
	        hql.append(",isHotre=?");
	        hql.append(",upfileDomain=?");
	        hql.append(",topicCheckFlag=?");
	        hql.append(",menuOpenFlag=?");
	        
	        super.updateObject(hql.toString(), new Object[]{sysInfo.getSysName(),
	        	sysInfo.getSysUrl(),
	        	sysInfo.getSysTitle(),
	        	sysInfo.getSysMetaKey(),
	        	sysInfo.getSysMetaDesc(),
	        	sysInfo.getCopyright(),
	        	sysInfo.getSmtpServer(),
	        	sysInfo.getSmtpId(),
	        	sysInfo.getSmtpPass(),
		        sysInfo.getSmtpAuth(),
		        sysInfo.getSmtpPort(),
		        sysInfo.getSmtpSsl(),
		        sysInfo.getSysEmail(),
		        sysInfo.getSysEmailSign(),
		        sysInfo.getUpfileOpenFlag(),
		        sysInfo.getUpfileMaxSize(),
		        sysInfo.getUpfileDefaultSize(),
		        sysInfo.getUpfileExt(),
		        sysInfo.getVersion(),
		        sysInfo.getUserpicOpenFlag(),
		        sysInfo.getRefreshPage(),
		        sysInfo.getIsHottopic(),
		        sysInfo.getIsHotre(),
		        sysInfo.getUpfileDomain(),
		        sysInfo.getTopicCheckFlag(),
		        sysInfo.getMenuOpenFlag()});
            OSCacheManager.removeCacheObject("SYS_INFO");   //缓存失效  
	}

	/**
     * 更新注册协议
	 * @param regAgree
	 */
	public void updateRegAgree(String regAgree)
	{
		String hql = "update TbbsSysInfo set regAgree=?";
		super.updateObject(hql,new Object[]{regAgree});
        OSCacheManager.removeCacheObject("SYS_INFO");   //缓存失效 
	}
	
	/**
     * 更新社区回复表的最后一条主题的ID
	 * @param lastReId
	 */
	public void updateLastReId(int lastReId)
	{		
		String hql = "update TbbsSysInfo set lastReID="+lastReId;
		super.executeHql(hql); 
        OSCacheManager.removeCacheObject("SYS_INFO");   //缓存失效 
	}

}
